/**
 * 首页
 */

$(function () {

    $("#search_btn").on("click",function(){
        // 搜索条件
        var text = $("#search_text").val();
        if(text){
            var url = encodeURI("search_result.html?text="+text);
            window.location.href = url;
        }
    })

    // 查看下详情
    $("#view_detail").on("click",function(){
        var id = $(".swiper-slide-active").attr("id");
        var exStatus = $("#exStatus").text();
        var url = encodeURI("info.html?id=" + id +"&type=TZGGHDTZ&exStatus="+exStatus);
        window.location.href = url;

    })

    // 获取悬浮窗
    function getBox() {
        let params = {
            url: "/cmsFloatBox/box",
            headers: {token: "111"},
            sCallback: function (dataList) {
                if (dataList) {
                    for(var i=0;i<dataList.length;i++){
                        var data = dataList[i];
                        var url = imageUrl + data.imgUrl;
                        // url = "http://106.14.172.38:8990/jmrhupload/cover/153932250322507637.jpg";
                        var content = data.content;
                        // $("#fdck").css("background","url("+url+") no-repeat");
                        // $("#fdck").css("background-size","100% 100%;");
                        if(content && str.indexOf("<br/>") != -1){
                            content = content.replace(/\n/g,"<br/>");
                        }
                        // $("#floatBox_text").html(content);
                        var linkUrl = data.linkUrl;
                        // $("#floatBox").attr("href",linkUrl);

                        var html = " <a class='floatBox' href='"+linkUrl+"'>" +
                            "            <div class='fdck' style='background:url("+url+") no-repeat;background-size:100% 100%;'>" +
                            "                <span class='floatBox_text'>" +content+
                            "                </span>" +
                            "            </div>" +
                            "         </a>";
                        $("#boxContent").append(html);
                        var blankHtml = "<div style='height:5px;margin:0 auto;'></div>"
                        $("#boxContent").append(blankHtml);
                    }
                    // 开始悬浮窗 上下浮动
                    start();
                }
            },
            eCallback: function (res) {
            }
        }
        requset(params);
    }

    // 获取悬浮窗
    getBox();
});

//图片前置url
var pre = imageUrl;

//首页轮播
$(function(){

    $.ajax({
        type: "GET",
        url: restUrl + "/index/items",   //接口地址
        dataType: "json",
        data: {
            parentCode :"SYLB",
            loadAll:true,
            needPaging :false,
        },
        success:function(data){
            if(data.code == 10000){
                for(var i=0;i<data.data.rows.length;i++){
                    var item=data.data.rows[i];
                    var d = item.contentUrl !="" ?  item.contentUrl : "#";
                    $('#cbb').append('<li></li>')
                    if(d != "#"){
                        $('#cbd').append('<li><a href="'+ d + '"><img src="'+ pre + item.cover + '"></a></li>')
                    } else {
                        $('#cbd').append('<li><img src="'+ pre + item.cover + '"></li>')
                    }


                }

                //--**  开启首页第一屏轮播图特效  **--//
                $(".banner-slide").slide({mainCell: ".bd ul", autoPlay: true, delayTime: 5000, interTime: 5000});

            }else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    })
});

//要闻动态
$(function () {

    $.ajax({
        type: "GET",
        url: restUrl + "/index/items",   //接口地址
        dataType: "json",
        data: {
            parentCode: "YWDT",
            loadAll: true,
            needPaging: false,
        },
        success: function (data) {
            if (data.code == 10000) {

                var obj = data;
                for (var i = 0; i < 3; i++) {
                    var item = obj.data.rows[i];
                    if(item.description.length >35){
                        item.description = item.description.substr(0,35) + '...';
                    }
                    if(item.title.length >30){
                        item.title = item.title.substr(0,30) + '...';
                    }
                    if(item.copied ==0){
                        $('#contentarea').append('<li class="span12" style="margin-bottom: 10px"><a href="info.html?id=' + item.id + '&type=YWDT" class="yulanPic"><img src="' + pre + item.cover + '"></a>' +
                            '<div class="xinwen"><a href="info.html?id=' + item.id + '&type=YWDT">' + item.title + '</a><span>发布时间：' + formatDate(item.publishDate) + '&nbsp;&nbsp;作者：' + item.author + '</span>' +
                            '<div class="small_font">' + item.description + '</div></div></li>')
                    } else {
                        $('#contentarea').append('<li class="span12" style="margin-bottom: 10px"><a href="info.html?id=' + item.id + '&type=YWDT" class="yulanPic"><img src="' + pre + item.cover + '"></a>' +
                            '<div class="xinwen"><a href="info.html?id=' + item.id + '&type=YWDT">' + item.title + '</a><span>发布时间：' + formatDate(item.publishDate) + '&nbsp;&nbsp;来源：' + item.copyFrom + '</span>' +
                            '<div class="small_font">' + item.description + '</div></div></li>')
                    }

                }

            } else {
                console.log(data.message)
            }
        },
        error: function (data) {
            console.log(data)
        }
    })
});

//平台公告
$(function () {

    $.ajax({
        type: "GET",
        url: restUrl + "/index/items",   //接口地址
        dataType: "json",
        data: {
            parentCode: "TPGG",
            loadAll: false,
            needPaging: true,
            currPage: 1,
            pageSize: 3
        },
        success: function (data) {
            if (data.code == 10000) {

                for (var i = 0; i < data.data.rows.length; i++) {
                    var item = data.data.rows[i];
                    if(item.title.length >30){
                        item.title = item.title.substr(0,30) + '...';
                    }
                    $('#contentarea2').append('<li class="span12" style="margin-bottom: 5px"><a href="info.html?id=' + item.id + '&type=TPGG" style="width: 80%">' +
                        '<i class="fa fa-angle-right"></i>' + item.title + '</a>' +
                        '<div style="font-size: 12px; color: #ccc; float: right; margin-top: 3px;">' + formatDate(item.publishDate) + '</div></li>');
                }
            } else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    })
});

//活动介绍
$(function () {

    $.ajax({
        type: "GET",
        url: restUrl + "/index/exchanges",   //接口地址
        dataType: "json",
        data: {
            loadAll: false,
            needPaging: true,
            currPage: 1,
            pageSize: 3
        },
        success: function (data) {
            if (data.code == 10000) {

                for(var i=0;i<data.data.rows.length;i++){

                    var item = data.data.rows[i];

                    var stage = {
                      '未开始':'grey',
                      '报名中':'blue',
                      '进行中':'limegreen',
                      '已结束':'red',
                      '即将开始':'rgba(0,212,101,.8)',
                      '进行中,可报名':'rgba(164,66,241,.8)',
                    };

                    if(item.exStatus in stage){
                        $('.swiper-wrapper').append('<div class="swiper-slide" id='+item.id+'><div class="news_center_list"><div class="activity_registration_title"><h3>' + item.title + '</h3></div>' +
                            '<div class="activity_registration_descrition"><div class="dec_title">活动介绍&nbsp;&nbsp;&nbsp;&nbsp;<span style="background:#ffe3da;color: '+ stage[item.exStatus] + ';font-size:12px;padding:3px" id="exStatus">'+ item.exStatus + '</span></div>'+
                            '<a href="activity.html"><div class="activity_list_more">更多活动</div></a><div class="dec_content">' +
                            '<div style="width:100%">' +
                                '<div style="width:50%;display:inline-block;">活动时间：' + formatDate(item.exStart) +'---'+  formatDate(item.exEnd) + '</div>' +
                                '<div style="width:50%;display:inline-block;">活动报名时间：' + formatDate(item.enrollStart) + '---' + formatDate(item.enrollEnd) + '</div>' +
                            '</div>'+
                            '<div style="width:100%">' +
                                '<div style="width:50%;display:inline-block;">主办方名称：' + item.organizerName + '</div>' +
                                '<div style="width:50%;display:inline-block;">活动地址：' + item.exAddr + '</div>' +
                            '</div>'+
                            '<div style="width:100%">' +
                                '<div style="width:50%;display:inline-block;">主办方联系人：' + item.orgContacts + '</div>' +
                                '<div style="width:50%;display:inline-block;">主办方联系电话：' + item.orgPhone + '</div>' +
                            '</div>'+
                            '</div></div></div></div>');
                    }

                }

                //--**  开启首页活动轮播特效 **--//
                var mySwiper = new Swiper('.swiper-container', {
                    loop: true,
                    autoplay: true,
                    autoplayDisableOnInteraction:false,
                    pagination: {
                        el: '.swiper-pagination'
                    },
                    paginationClickable:true,
                    observer:true,//修改swiper自己或子元素时，自动初始化swiper
                    observeParents:true,//修改swiper的父元素时，自动初始化swiper
                    onSlideChangeEnd: function(swiper){
                        swiper.update();
                        mySwiper.startAutoplay();
                        mySwiper.reLoop();
                    }
                })

            } else {
                console.log(data.message)
            }

        },
        error:function (data) {
            console.log(data)
        }
    })
});

//数据统计
$(function () {

    //需求总量
    $.ajax({

        type: 'post',
        url: restUrl + "/index/libcount",
        dataType: 'json',
        contentType: 'application/json;charset=UTF-8',
        data: JSON.stringify({
            formType: "xq",
            site: "1",
            sex: "0",
            domain: "1",
            education: "0",
            degree: "0",
            sites: null,
            sexs: null,
            domains: null,
            educations: null,
            degrees: null
        }),
        success: function (data) {
            if (data.code == 10000) {
                var sum = 0;
                for (var i = 0; i < data.data.lib.length; i++) {
                    var item = data.data.lib[i];
                    sum += item.count;
                }
                $("#xq").html($('#xq').html() + sum);
            }else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    });

    //成果总量
    $.ajax({

        type: 'post',
        url: restUrl + "/index/libcount",
        dataType: 'json',
        contentType: 'application/json;charset=UTF-8',
        data: JSON.stringify({
            formType:"cg",
            site:"1",
            sex:"0",
            domain:"1",
            education:"0",
            degree:"0",
            sites:null,
            sexs:null,
            domains:null,
            educations:null,
            degrees:null
        }),
        success:function(data){
            if(data.code==10000){
                var sum = 0;
                for(var i=0;i<data.data.lib.length;i++){
                    var item=data.data.lib[i];
                    sum+=item.count;
                }
                $("#cg").html($('#cg').html() + sum);
            } else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    });

    //专家总量
    $.ajax({
        type: 'post',
        url:restUrl + "/index/libcount",   //接口地址
        dataType: 'json', //表示返回值的数据类型
        contentType: 'application/json;charset=UTF-8', //内容类型
        traditional: true, //使json格式的字符串不会被转码
        data: JSON.stringify({
            formType:"zj",
            site:"1",
            sex:"0",
            domain:"1",
            education:"0",
            degree:"0",
            sites:null,
            sexs:null,
            domains:null,
            educations:null,
            degrees:null
        }),
        success:function(data){
            if(data.code==10000){
                var sum = 0;
                for(var i=0;i<data.data.lib.length;i++){
                    var item=data.data.lib[i];
                    sum+=item.count;
                }
                $("#zj").html($('#zj').html() + sum);
            }else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    });

    //企业总量
    $.ajax({
        type: 'post',
        url:restUrl + "/index/libcount",   //接口地址
        dataType: 'json', //表示返回值的数据类型
        contentType: 'application/json;charset=UTF-8', //内容类型
        traditional: true, //使json格式的字符串不会被转码
        data: JSON.stringify({
            formType:"qy",
            site:"1",
            sex:"0",
            domain:"1",
            education:"0",
            degree:"0",
            sites:null,
            sexs:null,
            domains:null,
            educations:null,
            degrees:null
        }),
        success:function(data){
            if(data.code==10000){
                var sum = 0;
                for(var i=0;i<data.data.lib.length;i++){
                    var item=data.data.lib[i];
                    sum+=item.count;
                }
                $("#qy").html($('#qy').html() + sum);
            }else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    });

    //高校总量
    $.ajax({
        type: 'post',
        url:restUrl + "/index/libcount",   //接口地址
        dataType: 'json', //表示返回值的数据类型
        contentType: 'application/json;charset=UTF-8', //内容类型
        traditional: true, //使json格式的字符串不会被转码
        data: JSON.stringify({
            formType:"gx",
            site:"1",
            sex:"0",
            domain:"1",
            education:"0",
            degree:"0",
            sites:null,
            sexs:null,
            domains:null,
            educations:null,
            degrees:null
        }),
        success:function(data){
            if(data.code==10000){
                var sum = 0;
                for(var i=0;i<data.data.lib.length;i++){
                    var item=data.data.lib[i];
                    sum+=item.count;
                }
                $("#gx").html($('#gx').html() + sum);
            }else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    });

    //服务机构总量
    $.ajax({
        type: 'post',
        url:restUrl + "/index/libcount",   //接口地址
        dataType: 'json', //表示返回值的数据类型
        contentType: 'application/json;charset=UTF-8', //内容类型
        traditional: true, //使json格式的字符串不会被转码
        data: JSON.stringify({
            formType:"fwjg",
            site:"1",
            sex:"0",
            domain:"1",
            education:"0",
            degree:"0",
            sites:null,
            sexs:null,
            domains:null,
            educations:null,
            degrees:null
        }),
        success:function(data){
            if(data.code==10000){
                var sum = 0;
                for(var i=0;i<data.data.lib.length;i++){
                    var item=data.data.lib[i];
                    sum+=item.count;
                }
                $("#fwjg").html($('#fwjg').html() + sum);
            } else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    });

    //对接总量，对接成功总量，注册用户总量，网站访问量
    $.ajax({
        type: "GET",
        url: restUrl + "/index/vmucount",   //接口地址
        dataType: "json",
        data: {},
        success:function(data){
            if(data.code==10000){
                var meetingCnt=data.data.meetingCnt;
                var successMeetingCnt=data.data.successMeetingCnt;
                var userCnt=data.data.userCnt;
                var visitCnt=data.data.visitCnt;

                $("#meetingCnt").html($('#meetingCnt').html() +meetingCnt);
                $("#successMeetingCnt").html($('#successMeetingCnt').html() +successMeetingCnt);
                $("#userCnt").html($('#userCnt').html() +userCnt);
                $("#visitCnt").html($('#visitCnt').html() +visitCnt);
            } else {
                console.log(data.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    });

});

//转换日期格式
function formatDate(v) {
    if (/^(-)?\d{1,10}$/.test(v)) {
        v = v * 1000;
    } else if (/^(-)?\d{1,13}$/.test(v)) {
        v = v * 1;
    }
    var dateObj = new Date(v);
    var month = dateObj.getMonth() + 1;
    var day = dateObj.getDate();
    var hours = dateObj.getHours();
    var minutes = dateObj.getMinutes();
    var seconds = dateObj.getSeconds();
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    <!--if(hours<10){-->
    <!--hours = "0" + hours;-->
    <!--}-->
    <!--if(minutes<10){-->
    <!--minutes = "0" + minutes;-->
    <!--}-->
    <!--if(seconds<10){-->
    <!--seconds = "0" + seconds;-->
    <!--}-->
    <!--var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' +day + ' ' + hours + ':' + minutes + ':' + seconds;-->
    var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' + day + ' ';
    return UnixTimeToDate;
}

$(document).ready(function () {

    $.ajax({
        type: 'get',
        url: restUrl + "/index/recordvisit",
        dataType: 'json',
        data: {},
        success: function (v) {
            if (v.code != 10000) {
                console.log(v.message)
            }
        },
        error: function (data) {
            console.log(data)
        }
    });

});
